<?php
class ContentController extends MyController
{
	public $objname = '';
	public function init()
	{
		parent::init();
	}

	public function actionIndex()
	{
		$objname = ucwords($this->objname);
		$page = intval(Yii::app()->request->getParam('pageNum')) - 1;

		// 查库
		$CDbCriteria = new CDbCriteria;
		$CDbCriteria->order = "id asc";

		// 分页
		$count = $objname::model()->count($CDbCriteria);
		$pages = new CPagination($count);
		$pages->pageSize = self::PAGE_SIZE;
		$pages->currentPage = $page;
		$pages->applyLimit($CDbCriteria);

		$rows = $objname::model()->findAll($CDbCriteria);

		// 显示
		$data = array();
		$data['objname'] = $this->objname;
		$data['rows'] = $rows;
		$data['pages'] = $pages;
		$this->display('/content/index', $data);
	}
	
	public function actionAdd()
	{
		$objname = ucwords($this->objname);
		$category_name = $objname.'_category';
		$relation_name = $category_name.'_relation';
		if(Yii::app()->request->getParam('action') == 'add')
		{
			foreach (Yii::app()->params['langs'] as $lang => $name) {
				$objects[$lang] = BaseModel::getNewInstance($objname,$lang);
			}
			$categorys = Yii::app()->request->getParam('category');
			$title = Yii::app()->request->getParam('title');
			$content = Yii::app()->request->getParam('content');
			$order_id = intval(Yii::app()->request->getParam('order_id'));
			$thumbnail = MyImgUploader::upload('thumbnail',Yii::app()->params['imgpath'],Yii::app()->params['imgdomain']);
			if(empty($categorys))
			{
				$this->alert_error('请选择一个分类');
			}
			foreach ($objects as $lang => $val) {
				BaseModel::setLang($lang);
				$val->title = trim($title[$lang]);
				$val->content = trim($content[$lang]);
				if($thumbnail['url'])
				{
					$val->thumbnail = $thumbnail['url'];
				}
				$val->update_time = date('Y-m-d H:i:s');
				$val->order_id = $order_id;
				if($val->save())
				{
					$relation_name::model()->deleteAll("obj_id=$id");
					foreach ($categorys as $category_id)
					{
						$category = BaseModel::getModel($category_name,$lang)->findByPk($category_id);
						if(!$category)
						{
							$this->alert_error('分类'.$category_id.'不存在');
						}
						else 
						{
							$relation_name::model()->newrecord($val->id,$category_id);
						}
					}
				}
				else
				{
					$this->alert_error();
				}
			}
			$this->alert_ok('发布成功');
		}
		else
		{
			$data = array();
			$data['categorys'] = $category_name::model()->findAll();
			$this->display('/content/add', $data);
		}
	}
	
	public function actionEdit()
	{
		$objname = ucwords($this->objname);
		$category_name = $objname.'_category';
		$relation_name = $category_name.'_relation';
		$id = intval(Yii::app()->request->getParam('id'));
		foreach (Yii::app()->params['langs'] as $lang => $name) {
			$objects[$lang] = BaseModel::getModel($objname,$lang)->findByPk($id);
		}
		if(!$id || !$objects)
		{
			$this->alert_error('您选择的文章不存在');
		}
		if(Yii::app()->request->getParam('action') == 'edit')
		{
			$categorys = Yii::app()->request->getParam('category');
			$title = Yii::app()->request->getParam('title');
			$content = Yii::app()->request->getParam('content');
			$order_id = intval(Yii::app()->request->getParam('order_id'));
			$thumbnail = MyImgUploader::upload('thumbnail',Yii::app()->params['imgpath'],Yii::app()->params['imgdomain']);
			if(empty($categorys))
			{
				$this->alert_error('请选择一个分类');
			}
			foreach ($objects as $lang => $val) {
				BaseModel::setLang($lang);
				$val->title = trim($title[$lang]);
				$val->content = trim($content[$lang]);
				if($thumbnail['url'])
				{
					$val->thumbnail = $thumbnail['url'];
				}
				$val->update_time = date('Y-m-d H:i:s');
				$val->order_id = $order_id;
				if($val->save())
				{
					$relation_name::model()->deleteAll("obj_id=$id");
					foreach ($categorys as $category_id)
					{
						$category = BaseModel::getModel($category_name,$lang)->findByPk($category_id);
						if(!$category)
						{
							$this->alert_error('分类'.$category_id.'不存在');
						}
						else 
						{
							$relation_name::model()->newrecord($val->id,$category_id);
						}
					}
				}
				else
				{
					$this->alert_error();
				}
			}
			$this->alert_ok('发布成功');
		}
		else
		{
			$data = array();
			$data['categorys'] = BaseModel::getModel($category_name,Yii::app()->params['default_language'])->findAll();
			$data['obj'] = $objects;
			$selected_category = array();
			foreach ($objects[Yii::app()->params['default_language']]->rels as $r)
			{
				$selected_category[] = $r->category_id;
			}
			$data['selected_category'] = $selected_category;
			$this->display('/content/edit', $data);
		}
	}
	
	public function actionDelete()
	{
		$objname = ucwords($this->objname);
		$category_name = $objname.'_category';
		$relation_name = $category_name.'_relation';
		$id = intval(Yii::app()->request->getParam('id'));
		$object = $objname::model()->findByPk($id);
		if(!$id || !$object)
		{
			$this->alert_error('您选择的文章不存在');
		}
		$relation_name::model()->deleteAll("obj_id=$id");
		$object->delete();
		$this->alert_ok();
	}
	public function actionCategory()
	{
		$category_name = ucwords($this->objname).'_category';
		$page = intval(Yii::app()->request->getParam('pageNum')) - 1;

		// 查库
		$CDbCriteria = new CDbCriteria;
		$CDbCriteria->order = "id asc";

		// 分页
		$count = $category_name::model()->count($CDbCriteria);
		$pages = new CPagination($count);
		$pages->pageSize = self::PAGE_SIZE;
		$pages->currentPage = $page;
		$pages->applyLimit($CDbCriteria);

		$rows = $category_name::model()->findAll($CDbCriteria);

		// 显示
		$data = array();
		$data['name'] = $category_name;
		$data['rows'] = $rows;
		$data['pages'] = $pages;
		$this->display('/content/category', $data);
	}
	
	public function actionCategory_add()
	{
		if(Yii::app()->request->getParam('submit'))
			$this->category_save();
		$this->display('/content/category_edit');
	}
	
	public function actionCategory_edit()
	{
		if(Yii::app()->request->getParam('submit'))
			$this->category_save();
		$id = intval(Yii::app()->request->getParam('id'));
		$categories = $this->getCategoryRecords($id);
		$this->display('/content/category_edit',array('id' => $id,'categories'=>$categories));
	}
	private function getCategoryRecords($id=0)
	{
		$objname = ucwords($this->objname);
		$category_name = $objname.'_category';
		$return = array();
		foreach (Yii::app()->params['langs'] as $lang => $name) {
			if($id)
			{
				$return[$lang] = BaseModel::getModel($category_name,$lang)->findByPk($id);
			}
			else
			{
				$return[$lang] = BaseModel::getNewInstance($category_name,$lang);
			}
		}
		return $return;
	}

	private function category_save()
	{
		$names = Yii::app()->request->getParam('name');
		$id = intval(Yii::app()->request->getParam('id'));
		$categories = $this->getCategoryRecords($id);
		foreach (Yii::app()->params['langs'] as $lang => $name) {
			if(!trim($names[$lang]))
				$this->alert_error($name.'分类名未填写');
			$categories[$lang]->name = trim($names[$lang]);
			$categories[$lang]->save();
		}
		$this->alert_ok('保存成功',array('callbackType'=>'closeCurrent'));
	}
}